package com.xiaoyao.novel.parser;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.xiaoyao.novel.crawl.BaseCrawl;
import com.xiaoyao.novel.pojo.Book;
import com.xiaoyao.novel.pojo.Chapter;
import com.xiaoyao.novel.util.ClassUtil;
import com.xiaoyao.novel.util.FileUtil;

public class QidianParser {
	private static Logger logger = Logger.getLogger(QidianParser.class);

	/**
	 * 作者,[书名],简介,[类型],图片
	 * 
	 * @param page
	 * @param book
	 * @return
	 */
	public static Book parseDescrib(String page, Book book) {
		page = page.replaceAll("\"", "'");
		page = page.replaceAll("\\s+", " ");
		logger.debug("页面: " + page);
		//		String infoRegex = "<a itemprop='url'\\s*stat-type='read'\\s*href='(.+?)'.+?>点击阅读</a>";
		String infoRegex = "<div class='book_pic'>.+?<img.+?src='(.+?)'.+?<a itemprop='url'\\s*stat-type='read'\\s*href='(.+?)'.+?>点击阅读</a>" +
				".+?小说作者.+?<span itemprop='name'>(.*?)</span>.+?<span itemprop='description'>(.+?)</span>";
		Pattern infoPattern = Pattern.compile(infoRegex);
		Matcher matcher = infoPattern.matcher(page);
		if (matcher.find()) {
			// book.bookName = matcher.group(1);
			if (ClassUtil.isEmpty(book.picPath)) {
				String picUrl = matcher.group(1);
				String fileName = book.bookId + ".jpg";
				fileName = BaseCrawl.downLoadImage(picUrl, fileName); // 抓图片
				book.picPath = FileUtil.PIC_DIR + fileName;
			}
			book.bookUrl= matcher.group(2);
			if (ClassUtil.isEmpty(book.authorName)  ) {
				book.authorName = matcher.group(3);
			}
			if (ClassUtil.isEmpty(book.description)) {
				String describ = matcher.group(4); 
				describ=URLEncoder.encode(describ);
				describ=describ.replaceAll("%C2%A0%C2%A0%C2%A0","");
				describ=URLDecoder.decode(describ);
				book.description =  describ ;
			}
		} else {
			logger.error("简介有问题!" + book.crawler);
		}

		return book;
	}

	public static List<Chapter> parseBook(String page, Book book) {
		logger.info("开始解析...");
		page = page.replaceAll("\"", "'");
		page = page.replaceAll("\\s+", " ");
//		page = page.replaceFirst(".+?id='content'", "");s
		List<Chapter> list = new ArrayList<Chapter>();
		book.list = list;
//		String bookRegex = "<a itemprop='url' href='(.+?,(.+?)\\.aspx)' title='.+?'><span itemprop='headline'>(.+?)</span></a>";
		String bookRegex = "<li data-rid='\\d+'.*?><a href='(.+?)'.+?>(.+?)</a>";
		Pattern bookPattern = Pattern.compile(bookRegex);
		Matcher matcher = bookPattern.matcher(page);
		while (matcher.find()) {
			String title = matcher.group(2).trim();
			String chapterUrl= matcher.group(1); 
			if (chapterUrl.contains("/chapter/")) {
				Chapter chapter = new Chapter(); 
				list.add(chapter);
				chapter.bookId = book.bookId;
				chapter.chapterUrl = matcher.group(1);
				chapter.webId = matcher.group(2);
				chapter.chapterTitle = title;
				logger.debug(chapter);
			}
		}
		return list;
	}

	public static void parseChapter(String page, Chapter chapter) {
		page = page.replaceAll("\"", "'");
		page = page.replaceAll("\\s+", "");
//		logger.warn(page );
		//百度的爬虫,起点会直接显示内容数据
//		String chapterRegex = "<divclass='bookcontent'id='content'style='.*?'><scriptsrc='(http:.+?txt)'charset='GB2312'></script>";
		String chapterRegex = "<divclass='read-contentj_readContent'>(.+?)</div>";
		Pattern chapterPattern = Pattern.compile(chapterRegex);
		Matcher matcher = chapterPattern.matcher(page);
		if (matcher.find()) {
//			chapter.chapterTxtUrl = matcher.group(1);
			chapter.chapterContent   = matcher.group(1);
			// logger.info(chapter.chapterTxtUrl);
		}else {
//			page=page.replaceFirst(".+?<divclass='bookreadercontent'id='chaptercontent'.*?>", "");
//			page=page.replaceFirst("</div>.+", "");
//			chapter.chapterContent   = page;
		}
	}
}
